//  定时巡检任务  定时对自身的组件进行巡检   及时的纠正数据
//  比如说定时检查和服务器之间的通信信息

class TimerTask{
    constructor(time){
        this.time = time ? time : 5000;
        this.interval = -1;
        this.valueMap = new Map();
    }

    start(){
        let that = this;
        this.interval = setInterval(() => {
            that.valueMap.forEach((v,k) => {
                try{
                    v.check();
                }catch (e) {
                    console.log(e);
                }
            })
        },this.time);
        return this;
    }

    stop(){
        clearInterval(this.interval);
    }

    register(name,value){
        if(name == null || value == null || value.check == null){
            return this;
        }
        this.valueMap.set(name,value);
        return this;
    }

    unregister(name){
        this.valueMap.delete(name);
        return this;
    }
}

export default TimerTask;